package com.ntanh.mealplanner.main.objects;

import android.content.ContentValues;
import android.util.Log;

import com.ntanh.mealplanner.base.model.NamedEntity;
import com.ntanh.mealplanner.main.dao.DataBaseHelper;

import org.json.JSONObject;

/**
 * Created by Anh Nguyen on 2/24/2015.
 */
public class Ingredient extends NamedEntity {

    public static final String UNIT = "unit";

    private String unit;

    public Ingredient(String author, String name, String image, String description, String unit) {
        super(author, name, image, description);
        this.unit = unit;
    }

    public Ingredient(String author) {
        super(author);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Ingredient)) return false;
        if (!super.equals(o)) return false;

        Ingredient that = (Ingredient) o;

        if (unit != null ? !unit.equals(that.unit) : that.unit != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + (unit != null ? unit.hashCode() : 0);
        return result;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    @Override
    public String toString() {
        return "";
    }

    public JSONObject toJSONObject() {

        JSONObject json = super.toJSONObject();
        try {
            if (json != null) {
                json = new JSONObject();
            }
            json.put(Ingredient.UNIT, unit);

        } catch (Exception e) {
            Log.d("Convert error", "Cannot convert Ingredient to JSON", e);
        }

        return json;
    }

    public void toContentValue(ContentValues values, boolean forInserting) {
        super.toContentValue(values, forInserting);
        values.put(UNIT, getUnit());
    }

    @Override
    public String getTableName() {
        return DataBaseHelper.INGREDIENT_TABLE;
    }
}
